//***********************************************************************
//	File:		Bullet.cpp
//	Author:		
//	Course:		SGP 1405
//	Purpose:	
//***********************************************************************
//***********************************************************************
//	Purposfully left blank for all comments regarding this file 
//	
//	
//	
//***********************************************************************



#include "Bullet.h"
#include "../source/Game.h"
#include "../GameStates/Play.h"

#include "../SGD Wrappers/SGD_GraphicsManager.h"

#include "../Message/DestroyEntityMessage.h"

Bullet::Bullet(float posX, float posY, Entity* owner)
{
	SetDamage(20); 
	//SetFinalSound();
	SetHasCollided(false); 
	SetImage(Game::GetInstance()->GetBulletImg());
	SetOwnerType(owner); 
	SetPosition(SGD::Point{ posX, posY }); 
	SetRotation(owner->GetRotation());
	SetSize(SGD::Size{ 4, 4 });
	SetSpeed(200);
	SetVelocity(SGD::Vector{ 0, 0 }); 
}

void Bullet::Update(float dt)
{
	SGD::Rectangle cameraPos = Play::GetInstance()->GetCameraRect();

	if (m_ptPosition.x < cameraPos.left
		|| m_ptPosition.x + m_szSize.width > cameraPos.right
		|| m_ptPosition.y < cameraPos.top
		|| m_ptPosition.y + m_szSize.height > cameraPos.bottom)
	{
		SetOwnerType(nullptr); 
		DestroyEntityMessage* entityToDestroy = new DestroyEntityMessage(this); 
		entityToDestroy->QueueMessage(); 
		entityToDestroy = nullptr; 
	}

	SGD::Vector newVelocity = { 0, -1 }; 
	newVelocity.Rotate(GetRotation()); 
	newVelocity = newVelocity * GetSpeed();
	
	SetVelocity(newVelocity); 
	

	//Last line of function
	Entity::Update(dt); 
}

void Bullet::Render()
{
	SGD::GraphicsManager* draw = SGD::GraphicsManager::GetInstance(); 
	SGD::Point camera = Play::GetInstance()->GetCameraPos(); 

	SGD::Point playerPos = GetPosition();
	playerPos.x -= camera.x;
	playerPos.y -= camera.y;



	draw->DrawTexture(GetImage(), playerPos,
		GetRotation(), GetSize() / 2); 

}

void Bullet::HandleCollision(const IEntity* pOther)
{
	if (pOther != GetOwnerType())
	{
		DestroyEntityMessage* entityToDestroy =
			new DestroyEntityMessage(this); 

		entityToDestroy->QueueMessage(); 
		entityToDestroy = nullptr; 
	}
}

void Bullet::HandleEvent()
{

}

SGD::Rectangle Bullet::GetRect() const
{
	return SGD::Rectangle
	{
		GetPosition() - (GetSize() / 2), 
		GetPosition() + (GetSize() / 2)
	};
}